package com.kern.common.util;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.context.annotation.Configuration;

/**
 * Created by caoshi at 20:34 2021-09-15
 */
@Configuration
public class BeanUtil implements ApplicationContextAware {

    private static Logger logger = LoggerFactory.getLogger(BeanUtil.class);

    private static ApplicationContext applicationContext;

    public void setApplicationContext(ApplicationContext context) throws BeansException {
        applicationContext = context;
    }

    public static Object getBean(String classname) {
        if (applicationContext.containsBean(classname)) {
            Object bean = applicationContext.getBean(classname);
            return bean;
        }
        logger.error("bean {} 不存在", classname);
        return null;
    }


}


